<?php
session_start();
include '../conn.php';
include 'function.php';

$uid = $_REQUEST['uid'];

$sql = "SELECT * FROM user WHERE id = '" . $uid . "'";
$result = $conn->query($sql);

if ($result->num_rows <= 0) {
    echo '<div style="text-align: center; margin: 50px; font-size: 20px; color: red;">请先注册或登录！</div>';
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的收藏</title>
    <link rel="stylesheet" type="text/css" href="../css/general.css">
    <link rel="stylesheet" type="text/css" href="../css/index.css">
    <link rel="stylesheet" type="text/css" href="../css/table.css">
    <script type="text/javascript" src="../js/jquery.js"></script>
    <script type="text/javascript" src="../js/general.js"></script>
</head>
<body>
<!-- 头部开始 -->
<div class="header-body">
    <!-- 头部开始 -->
    <?php include 'header.php'; ?>
    <!-- 头部结束 -->
</div>
<!-- 头部结束 -->

<!-- 主体开始 -->
<div class="container w1100">
    <h2 style="text-align: center; margin-top: 20px;">我的收藏</h2>
    <table style="width: 100%; border-collapse: collapse; margin: 20px 0; border: 1px solid #ddd; background-color: #fff; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
        <thead>
            <tr style="background-color: #f1f1f1;">
                <th style="padding: 10px; border: 1px solid #ddd;">收藏ID</th>
                <th style="padding: 10px; border: 1px solid #ddd;">商品ID</th>
                <th style="padding: 10px; border: 1px solid #ddd;">用户ID</th>
                <th style="padding: 10px; border: 1px solid #ddd;">商品名字</th>
                <th style="padding: 10px; border: 1px solid #ddd;">商品描述</th>
            </tr>
        </thead>
        <tbody>
            <?php
            // 检查是否设置了所有必要的参数
            if (!isset($_REQUEST['uid'])) {
                echo "<tr><td colspan='6' style='padding: 10px; border: 1px solid #ddd; text-align: center; color: red;'>非法访问！</td></tr>";
                exit;
            }
            $uid = $_REQUEST['uid'];

            // 检查用户是否存在
            $sql = "SELECT * FROM user WHERE id = " . $uid;
            $result = $conn->query($sql);

            // 用户存在才能进行查询
            if ($result && $result->num_rows > 0) {
                // 准备SQL查询
                $sql2 = "SELECT c.*, g.goods_name, g.price, g.description, g.picture FROM collect c 
                         JOIN goods g ON c.good_id = g.id 
                         WHERE c.user_id = " . $uid;
                $result2 = $conn->query($sql2);
                if ($result2 && $result2->num_rows > 0) {
                    while ($comment = $result2->fetch_assoc()) {
                        echo "<tr style='background-color: #f9f9f9;'>";
                        echo "<td style='padding: 10px; border: 1px solid #ddd;'>" . htmlspecialchars($comment['id']) . "</td>";
                        echo "<td style='padding: 10px; border: 1px solid #ddd;'>" . htmlspecialchars($comment['good_id']) . "</td>";
                        echo "<td style='padding: 10px; border: 1px solid #ddd;'>" . htmlspecialchars($comment['user_id']) . "</td>";
                        echo "<td style='padding: 10px; border: 1px solid #ddd;'>" . htmlspecialchars($comment['goods_name']) . "</td>";
                        echo "<td style='padding: 10px; border: 1px solid #ddd;'>" . htmlspecialchars($comment['description']) . "</td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='6' style='padding: 10px; border: 1px solid #ddd; text-align: center;'>没有找到收藏的商品。</td></tr>";
                }
            } else {
                echo "<tr><td colspan='6' style='padding: 10px; border: 1px solid #ddd; text-align: center;'>没有找到用户或查询失败。</td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>
<!-- 主体结束 -->

<!-- 页脚开始 -->
<div style="margin-top:200px;"></div>
<?php include '../footer.php'; ?>
<!-- 页脚结束 -->
</body>
</html>